<template>
    <div class="pack">
        <div class="tree_container">
            <h3>字典数据列表</h3>
            <div class="treePack">
                <Tree :data="TreeData" @on-select-change="organizationTreeChange"></Tree>
            </div>
        </div>
        <div class="detail_container">
            <h3>字典数据维护</h3>
            <div class="hunttool">
                <!--<Input v-model="valueEsName" size="small" placeholder="存储点名称" class="hunt" ></Input>-->
                <!--<Button type="primary" icon="ios-search" size="small"  @click="searchData">搜索</Button>-->
                <!--<Button type="ghost" icon="ios-trash-outline" size="small" class="deltebtn" @click="batchDelete">批量删除</Button>-->
                <Button type="ghost" icon="ios-plus-empty" size="small" class="addbtn" @click="addnew">新增</Button>
            </div>
            <div class="compile">
                <div style="text-align: center;padding-top: 25%;font-size: 18px;" v-if="!detailShow">您还未选中任何子节点，请选中其中一个子节点进行操作！</div>
                <Table stripe  border :columns="columns" :data="tabledata" @on-selection-change="compileSelect" v-if="detailShow"></Table>
            </div>
            <div class="page_container" v-if="detailShow">
                <Page :current="pageNo" :total="totalCount" size="small" simple  class="page_btn" @on-change="pageChange"></Page>
                <P class="page_text">共{{totalCount}}条</P>
            </div>
        </div>
        <!--新增、查看、修改弹出框-->
        <Modal v-model="compile" :title="compiletitle" :closable="false" :mask-closable="false">
            <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="100">
                <Form-item label="属性名称" prop="value">
                    <Input v-model="formValidate.value" placeholder="请输入属性名称"></Input>
                </Form-item>
                <Form-item label="说明" prop="description">
                    <Input v-model="formValidate.description" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder=""></Input>
                </Form-item>
            </Form>
            <div slot="footer">
                <Button type="ghost" @click="compileCancel()">取消</Button>
                <Button type="primary" @click="compileSubmit('formValidate')">提交</Button>
            </div>
        </Modal>
    </div>
</template>
<script type="text/javascript">
    var ezjsUtil = Vue.ezjsUtil;
    var apiConstants = ezjsUtil.constants.api;
    export default {
        components: {},
        data() {
            return {
                compile: false,
                detailShow:false,
                compiletitle:"字典数据维护",
                //搜索存放点
                valueEsName:'',
                addkeyCopy:'',
                //树列表数据
                TreeData:[],
                //分页页数
                pageNo:1,
                pageNoid:'',
                //分页共多条记录
                totalCount:0,
                //批量删除ID
                compiledele:'',
                columns: [
                    {
                        type: 'index',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '属性名称',
                        key: 'value',
                    },
                    {
                        title: '说明',
                        key: 'description'
                    },
                    {
                        title: '编辑',
                        key: 'action',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.amend(params.row);
                                        }
                                    }
                                }, '修改'),
                                h('Button', {
                                    props: {
                                        type: 'error',
                                        size: 'small'
                                    },
                                    on: {
                                        click: () => {
                                            this.dictionaryChildRemoveByIds(params.row.id,params.row.dictionaryId);
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }
                ],

                tabledata: [],
                formValidate: {
                    id:'',
                    keyCode: '',
                    description: '',
                    dictionaryId:'',
                    value: '',
                },
                ruleValidate: {
                    value: [
                        { required: true, whitespace: true, message: '请输入属性名称', trigger: 'blur' }
                    ],
                }
            }
        },
        computed: {},
        created() {
//            this.supplieStorageFlipList();
            this.dictionarysFlipList();
        },
        methods: {
            //提示信息
            instance () {
                const title = '提示';
                const content = '<p>请在字典数据列表选择子节点！</p>';
                this.$Modal.info({
                    title: title,
                    content: content
                });
            },
            //点击事件
            organizationTreeChange(val){
                if(val.length>0){
                    if(val[0].children){
                        this.detailShow=false;
                    }else {
                        this.detailShow=true;
                    }
                    this.dictionaryChildFlipListForDictionaryId(val[0].id);
                    this.pageNoid=val[0].id;
                    this.dictionaryDetail(val[0].id);
                }else {
                    this.detailShow=false;
                }
//                alert(this.formValidate.departmentName);
            },
            //搜索
            searchData(){
                this.pageNo=1;
//                this.supplieStorageFlipList();
            },
            //新增
            addnew(){
                if( this.detailShow){
                    this.formValidate.keyCode=this.addkeyCopy.keyCode;
                    this.formValidate.dictionaryId=this.addkeyCopy.id;
                    this.compile = true;
                }else {
                    this.instance ();
                }
            },
            //修改
            amend(val){
                this.formValidate.id=val.id;
                this.formValidate.keyCode=val.keyCode;
                this.formValidate.description=val.description;
                this.formValidate.dictionaryId=val.dictionaryId;
                this.formValidate.value=val.value;
                this.compile = true;
            },
            //取消
            compileCancel(){
                this.compileReset('formValidate');
                this.compile=false;
            },
            //分页改变时
            pageChange(val){
                this.pageNo=val;
                this.dictionaryChildFlipListForDictionaryId(this.pageNoid);
            },
            //表单重置
            compileReset (name) {
                this.$refs[name].resetFields();
            },
            //表格勾选
            compileSelect(selection,row){
                var ids="";
                for (var i=0;i<selection.length;i++){
                    ids=selection[i].id+","+ids;
                }
                this.compiledele=ids
            },
            //批量删除
            batchDelete(){
//                this.supplieStorageRemoveByIds(this.compiledele);
            },
            //新增或修改
            compileSubmit (name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        var _this = this;
                        ezjsUtil.request(apiConstants.dictionaryChild_edit,
                            {
                                id:_this.formValidate.id,
                                keyCode:_this.formValidate.keyCode,
                                description:_this.formValidate.description,
                                dictionaryId:_this.formValidate.dictionaryId,
                                value:_this.formValidate.value,
                            },
                            function (err, data) {
                                if (err) {
                                    return;
                                }
                                _this.dictionaryChildFlipListForDictionaryId(_this.addkeyCopy.id);
                                _this.pageNo=1;
                            }
                        )
                        _this.$Message.success('提交成功!');
                        _this.compile=false;
                        _this.compileReset('formValidate');
                        _this.formValidate.id='';
                    } else {
                        this.$Message.error('表单验证失败!');
                    }
                })
            },

            //查询
            dictionaryChildFlipListForDictionaryId(val){
                var _this = this;
                ezjsUtil.request(apiConstants.dictionaryChild_flipListForDictionaryId,
                    {
                        id:val,
                        pageNo:_this.pageNo
                    },
                    function (err, data) {
                        if (err) {
                            return;
                        }
                        _this.tabledata=data.listInfo;
                        _this.totalCount=data.flipInfo.totalCount;
//                        _this.RecordPage.totalPage=data.flipInfo.totalPage;

                    }
                )
            },

            //批量删除
            dictionaryChildRemoveByIds(delid , dictionaryId){
                var _this = this;
                ezjsUtil.request(apiConstants.dictionaryChild_removeByIds,
                    {
                        ids:delid
                    },
                    function (err, data) {
                        if (err) {
                            return;
                        }
                        _this.dictionaryChildFlipListForDictionaryId(dictionaryId);
//                        _this.supplieStorageFlipList();
                    }
                )
            },
            //获取所有字典树
            dictionarysFlipList() {
                var _this = this;
                ezjsUtil.request(apiConstants.dictionarys_flipList,
                    {  },
                    function (err, data) {
                        if (err) {
                            return;
                        }
                        _this.TreeData=data;
                        _this.TreeData[0].expand=true;
                    }
                )
            },
            //显示一个数据字典信息
            dictionaryDetail(val){
                var _this = this;
                ezjsUtil.request(apiConstants.dictionary_detail,
                    {
                        id:val
                    },
                    function (err, data) {
                        if (err) {
                            return;
                        }
                        _this.addkeyCopy=data;
                    }
                )
            }
        },
        mounted() {
        }
    }
</script>
<style lang="less" scoped>
    .pack{
        width: 100%;
        height: 100%;
    }
    .tree_container{
        width: 260px;
        height: 100%;
        float: left;
        border: 1px solid #d3d8e0;
    h3{
        line-height: 40px;
        text-align: center;
        font-size: 14px;
        color: #6e6d6d;
    }
    }
    .detail_container{
        height: 100%;
        margin-left: 280px;
        overflow: hidden;
        overflow-y: auto;
    }
    h3 {
        height: 40px;
        line-height: 40px;
        border-bottom: 1px solid #d3d8e0;
    }
    .treePack{
        height: 94%;
        padding: 10px;
        overflow: auto;
    }
    .hunttool {
        height: 55px;
        padding:15px 0;
    .addbtn {
        margin-right: 6px;
        float: right;
    }

    .deltebtn {
        margin-right: 6px;
        float: right;
    }

    }
    .hunt {
        width: 14rem;
    }
    .page_container{
        height: 30px;
        margin-top: 20px;
    .page_btn{
        float: right;
    }
    .page_text{
        float: right;
        line-height: 26px;
    }
    }
</style>